package com.example.roundcornerprogressbar.view.progressbar;

import ohos.agp.components.AttrSet;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.element.ShapeElement;
import ohos.app.Context;

public class CenteredRoundCornerProgressBar extends RoundCornerProgressBar {
    public CenteredRoundCornerProgressBar(Context context) {
        super(context);
    }

    public CenteredRoundCornerProgressBar(Context context, AttrSet attrSet) {
        super(context, attrSet);
    }

    public CenteredRoundCornerProgressBar(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
    }

    @Override
    protected void drawProgress(DirectionalLayout layoutProgress,
                                ShapeElement progressDrawable,
                                float max,
                                float progress,
                                float totalWidth,
                                int radius,
                                int padding,
                                boolean isReverse) {
        super.drawProgress(layoutProgress, progressDrawable, max, progress, totalWidth, radius, padding, isReverse);
        ComponentContainer.LayoutConfig params = (ComponentContainer.LayoutConfig)layoutProgress.getLayoutConfig();
        float ratio = max / progress;
        float progressWidth = (totalWidth - (padding * 2)) / ratio;
        float deltaWidth = totalWidth - progressWidth;
        params.setMargins((int) (deltaWidth / 2), params.getMarginTop(), (int) (deltaWidth / 2), params.getMarginBottom());
        layoutProgress.setLayoutConfig(params);
    }
}
